import { http } from '../http';
import type { ApiResponse } from '~/api/types/http';
import type {
  PaymentOrder,
  CreatePaymentParams,
  PaymentStatusResponse,
  PaymentHistoryParams,
  PaymentHistoryResponse,
  RefundParams,
  RefundResponse,
} from '~/api/types/payment';

/**
 * 支付API组合函数
 */
export const usePaymentApi = () => {
  /**
   * 创建支付订单
   * @param params 支付参数
   * @returns useFetch 响应对象
   */
  const createPayment = (params: CreatePaymentParams) =>
    http.post<PaymentOrder>('/payment/create', params);

  /**
   * 查询支付状态
   * @param paymentId 支付ID
   * @returns useFetch 响应对象
   */
  const getPaymentStatus = (paymentId: string) =>
    http.get<PaymentStatusResponse>(`/payment/status/${paymentId}`);

  /**
   * 取消支付
   * @param paymentId 支付ID
   * @returns useFetch 响应对象
   */
  const cancelPayment = (paymentId: string) =>
    http.put(`/payment/cancel/${paymentId}`);

  /**
   * 获取支付历史
   * @param params 查询参数
   * @returns useFetch 响应对象
   */
  const getPaymentHistory = (params: PaymentHistoryParams = {}) =>
    http.get<PaymentHistoryResponse>('/payment/history', { query: params });

  /**
   * 申请退款
   * @param params 退款参数
   * @returns useFetch 响应对象
   */
  const requestRefund = (params: RefundParams) =>
    http.post<RefundResponse>('/payment/refund', params);

  return {
    createPayment,
    getPaymentStatus,
    cancelPayment,
    getPaymentHistory,
    requestRefund,
  };
};
